#include "DaterMainCalendarManagerWindow.h"
#include "DaterCommon.h"
#include "DaterBaseWindowManager.h"
#include "DaterMainCalendarDayWindow.h"
#include "DaterMainCalendarMonthWindow.h"
#include "AnimationWindowTrans.h"
#include "DaterDaterWindow.h"
#include "DaterEventEditWindow.h"

MainCalendarManagerWindow::MainCalendarManagerWindow(const CEGUI::String& id_name, CEGUI::Window* parent, CEGUI::Window* child):
MainWindow(parent)
{
	using namespace CEGUI;

	_root = WindowManager::getSingleton().loadWindowLayout("CalendarManagerWindow.layout",id_name);

	_name = _root->getName().c_str();

	_root->setVisible(false);

	_root->setClippedByParent(false);

	_parent->addChildWindow(_root);

	EventEditWindow* eventEditWindow = new EventEditWindow("", System::getSingleton().getGUISheet());
	BaseWindowManager::getSingleton().addBaseWindow(eventEditWindow);

	MainCalendarMonthWindow* mainCalendarMonthWindow = new MainCalendarMonthWindow("", _root->getChild("CalendarManagerWindow/CalendarWindow"));
	BaseWindowManager::getSingleton().addBaseWindow(mainCalendarMonthWindow);

	_currentCalendarWindow = mainCalendarMonthWindow;

	MainCalendarDayWindow* mainCalendarDayWindow = new MainCalendarDayWindow("", _root->getChild("CalendarManagerWindow/CalendarWindow"));
	BaseWindowManager::getSingleton().addBaseWindow(mainCalendarDayWindow);

	WindowManager::getSingleton().getWindow("CalendarManagerWindow/CalendarManagerBar/HeadText")
		->setText(mbcs_to_utf8(_currentCalendarWindow->getHeadText().c_str()));

	subscribeEvents();
}

MainCalendarManagerWindow::~MainCalendarManagerWindow()
{
	BaseWindowManager::getSingleton().destroyBaseWindow("CalendarDayWindow");
	BaseWindowManager::getSingleton().destroyBaseWindow("CalendarMonthWindow");
	BaseWindowManager::getSingleton().destroyBaseWindow("EditEventWindow");

	CEGUI::WindowManager::getSingleton().destroyWindow(_root);
}

void MainCalendarManagerWindow::showWindow()
{
	RotateTransWindow* rotateTransWindow = new RotateTransWindow(_root, "rot2");
	rotateTransWindow->start();
	//_root->setVisible(true);
	//_root->activate();
}

void MainCalendarManagerWindow::hideWindow()
{
	_root->deactivate();
	RotateTransWindow* rotateTransWindow = new RotateTransWindow(_root, "rot1");
	rotateTransWindow->start();
}

void MainCalendarManagerWindow::subscribeEvents()
{
	using namespace CEGUI;

	WindowManager& wmgr = WindowManager::getSingleton();
	BaseWindowManager& bwmgr = BaseWindowManager::getSingleton();
	Window* but1 =
		wmgr .getWindow("CalendarManagerWindow/CalendarManagerBar/Month");
	Window* but2 =
		wmgr .getWindow("CalendarManagerWindow/CalendarManagerBar/Day");
	Window* but3 =
		wmgr .getWindow("CalendarManagerWindow/CalendarManagerBar/ExitButton");

	Window* but4 = 
		wmgr.getWindow("CalendarManagerWindow/CalendarManagerBar/Previous");

	Window* but5 = 
		wmgr.getWindow("CalendarManagerWindow/CalendarManagerBar/Next");

	Window* but6 = 
		wmgr.getWindow("CalendarManagerWindow/CalendarManagerBar/AddEventButton");

	but1->subscribeEvent(PushButton::EventClicked, Event::Subscriber(&MainCalendarManagerWindow::setToMonthWindow, this));

	but2->subscribeEvent(PushButton::EventClicked, Event::Subscriber(&MainCalendarManagerWindow::setToDayWindow, this));

	but3->subscribeEvent(Window::EventMouseClick,Event::Subscriber(&MainCalendarManagerWindow::backToIconWindow,this));

	but4->subscribeEvent(Window::EventMouseClick,Event::Subscriber(&MainCalendarManagerWindow::setToPreCalendarWindow, this));

	but5->subscribeEvent(Window::EventMouseClick,Event::Subscriber(&MainCalendarManagerWindow::setToNextCalendarWindow, this));

	but6->subscribeEvent(Window::EventMouseClick,Event::Subscriber(&MainCalendarManagerWindow::handle_addEvent, this));

	//_root->subscribeEvent(AnimationInstance::EventAnimationEnded, Event::Subscriber(&MainCalendarManagerWindow::handle_releaseAnimationInstance, this));
}

bool MainCalendarManagerWindow::backToIconWindow(const CEGUI::EventArgs &args){
	static_cast<DaterWindow*>(BaseWindowManager::getSingleton().getWindow("Root"))->backToPreviousWindow();

	return true;
}

bool MainCalendarManagerWindow::setToDayWindow(const CEGUI::EventArgs &args)
{
	if(!(_currentCalendarWindow == BaseWindowManager::getSingleton().getWindow("CalendarDayWindow")))
	{
		BaseWindowManager& BaseWmg = BaseWindowManager::getSingleton();

		MainCalendarDayWindow* calendarDayWidow = static_cast<MainCalendarDayWindow*>(BaseWmg.getWindow("CalendarDayWindow"));
		
		_currentCalendarWindow->hideWindow();
		calendarDayWidow->showWindow();

		_currentCalendarWindow = calendarDayWidow;

		CEGUI::WindowManager::getSingleton().getWindow("CalendarManagerWindow/CalendarManagerBar/HeadText")
			->setText(mbcs_to_utf8(_currentCalendarWindow->getHeadText().c_str()));
	}
	return true;
}

bool MainCalendarManagerWindow::setToMonthWindow(const CEGUI::EventArgs &args)
{
	if(!(_currentCalendarWindow == BaseWindowManager::getSingleton().getWindow("CalendarMonthWindow")))
	{
		BaseWindowManager& BaseWmg = BaseWindowManager::getSingleton();

		MainCalendarMonthWindow* calendarMonthWidow = static_cast<MainCalendarMonthWindow*>(BaseWmg.getWindow("CalendarMonthWindow"));

		_currentCalendarWindow->hideWindow();
		calendarMonthWidow->showWindow();

		_currentCalendarWindow = calendarMonthWidow;

		CEGUI::WindowManager::getSingleton().getWindow("CalendarManagerWindow/CalendarManagerBar/HeadText")
			->setText(mbcs_to_utf8(_currentCalendarWindow->getHeadText().c_str()));
	}
	return true;
}

bool MainCalendarManagerWindow::setToAgendaWindow(const CEGUI::EventArgs &args)
{
	//BaseWindowManager& BaseWmg = BaseWindowManager::getSingleton();

	//BaseWmg.getWindow(CEGUI::WindowManager::getSingleton().getWindow("CalendarManagerWindow/CalendarWindow")
	//	->getChild(0)->getName().c_str())->hideWindow();

	//BaseWmg.getWindow("CalendarAgendaWindow")->showWindow();

	//_currentCalendarWindow = static_cast<MainCalendarDayWindow*>(BaseWmg.getWindow("CalendarAgendaWindow"));

	//CEGUI::WindowManager::getSingleton().getWindow("CalendarManagerWindow/CalendarManagerBar/HeadText")
	//	->setText(mbcs_to_utf8(_currentCalendarWindow->getHeadText().c_str()));

	return true;
}

bool MainCalendarManagerWindow::setToNextCalendarWindow(const CEGUI::EventArgs &args)
{
	_currentCalendarWindow->setToNext(args);

	CEGUI::WindowManager::getSingleton().getWindow("CalendarManagerWindow/CalendarManagerBar/HeadText")
		->setText(mbcs_to_utf8(_currentCalendarWindow->getHeadText().c_str()));

	return true;
}

bool MainCalendarManagerWindow::setToPreCalendarWindow(const CEGUI::EventArgs & args)
{
	_currentCalendarWindow->setToPrevious(args);

	CEGUI::WindowManager::getSingleton().getWindow("CalendarManagerWindow/CalendarManagerBar/HeadText")
		->setText(mbcs_to_utf8(_currentCalendarWindow->getHeadText().c_str()));

	return true;
}

MainCalendarWindow* MainCalendarManagerWindow::getCurrentCalendarWindow()
{
	return _currentCalendarWindow;
}

bool MainCalendarManagerWindow::handle_releaseAnimationInstance(const CEGUI::EventArgs& args)
{
	const CEGUI::AnimationEventArgs& animationArgs = static_cast<const CEGUI::AnimationEventArgs&>(args);

	CEGUI::AnimationManager::getSingleton().destroyAnimationInstance(animationArgs.instance);

	return true;
}

bool MainCalendarManagerWindow::handle_addEvent(const CEGUI::EventArgs& args)
{
	BaseWindowManager::getSingleton().getWindow("EditEventWindow")->showWindow();

	return true;
}